﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Excell_On_Services.ViewModels.PagerModel>" %>
<%@ Import Namespace="Excell_On_Services.ViewModels"%>

<%
    if (Model.TotalItems <= Model.ItemsPerPage) 
        return;

    var totalPages = (Model.TotalItems / Model.ItemsPerPage) +
        ((Model.TotalItems % Model.ItemsPerPage == 0) ? 0 : 1);
    var urlTemplate = Model.PageUrlTemplate;
    var sb = new StringBuilder();
    
    if (Model.CurrentPageIndex > 0) {
        sb.Append(string.Format("<a {0} href=\"{1}\" /><span>{2}</span></a>", "", urlTemplate.Replace(PagerModel.PageSymbol, 0.ToString()), "&laquo; First"));
        sb.Append(string.Format("<a {0} href=\"{1}\" /><span>{2}</span></a>", "", urlTemplate.Replace(PagerModel.PageSymbol, (Model.CurrentPageIndex - 1).ToString()), "&laquo; Previous"));
    }
    
    for (int pageIndex = 0; pageIndex < totalPages; pageIndex++)
    {
        var @class = Model.CurrentPageIndex == pageIndex ? " class=\"number current\"" : "class=\"number\"";
        var link = urlTemplate.Replace(PagerModel.PageSymbol, pageIndex.ToString());
        sb.Append(string.Format("<a {0} href=\"{1}\" /><span>{2}</span></a>", @class, link, pageIndex + 1));
    }

    if (Model.CurrentPageIndex < (totalPages - 1)) {
        sb.Append(string.Format("<a {0} href=\"{1}\" /><span>{2}</span></a>", "", urlTemplate.Replace(PagerModel.PageSymbol,(Model.CurrentPageIndex + 1).ToString()), "Next &raquo;"));
        sb.Append(string.Format("<a {0} href=\"{1}\" /><span>{2}</span></a>", "", urlTemplate.Replace(PagerModel.PageSymbol, (totalPages - 1).ToString()) , "Last &raquo;"));
    }
    
    
    var html = sb.ToString();
    if (html != string.Empty)
    {
        Response.Write(sb);
    }
%>
